package w4156.idog.server;

import java.util.List;

import javax.ejb.EJBException;
import javax.ejb.Stateful;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;

import w4156.idog.model.User;

/**
 * Session Bean implementation class PlayerManagerBean
 */
@Stateful(mappedName = "ejb/eBlackjack/PlayerManagerSLSB")
public class PlayerManagerBean implements PlayerManagerBeanRemote {

    @PersistenceContext
    private EntityManager em;
//	private static PlayerManagerBean instance = null;

	//private List<UserSessionBean> gameusers;
	
    /**
     * Default constructor. 
     */
    public PlayerManagerBean() {
    }
    
/*	public static PlayerManagerBean getInstance() {
		if (instance == null) {
			instance = new PlayerManagerBean();
		}
		return instance;
	}
	*/
    @SuppressWarnings("unchecked")
    public List<User> findAllContacts(){
    	return em.createNamedQuery("User.findAllContacts").getResultList();
    }
    
    public User findByPK(int pk){
        User user = null;
        try{
        	user = (User)em.createNamedQuery("User.findByObjid").setParameter("objid",pk).getSingleResult();
        }catch(NoResultException e){
        	
        }catch(Exception e){
            throw new EJBException(e);
        }
      return user;    
    }
    
    @SuppressWarnings("unchecked")
	public List<User> findByName(String name){
    	List list = null;
    	try{
    		list = findAllContacts();
//    		list = em.createNamedQuery("User.findByName").setParameter("name","%"+name+"%").getResultList();
    		list = em.createNamedQuery("User.findByName").setParameter("name",name).getResultList();
    	}catch(Exception e){
    		throw new EJBException(e);
    	}
    	return list;
    }
    
    public void save(User user){
    	try{
    		em.merge(user);
    	}catch(Exception e){
    		throw new EJBException(e);
    	}
    	return;
    }
    
    public User update(User user){
    	try{
    		em.merge(user);
    	}catch(Exception e){
    		throw new EJBException(e);
    	}
    	return user;
    }
    
    /**
     * Will delete the user when user is offline
     */
    public void delete(User user){
		User tmpUsr = user;
		System.out.println("<EJB PMB>delete 1...");
		if (!em.contains(user)) {
			System.out.println("delete 2...");
			tmpUsr = (User)em.merge(user);
		}
		System.out.println("delete 3...");
		em.remove(tmpUsr);
    }

	@Override
	public int createUser(User user) {
	   	try{
    		em.merge(user);
    	}catch(Exception e){
    		throw new EJBException(e);
    	}
		return user.getObjid();
	}
	
	public void ModifyUser(User user){
		User tmpUsr = user;
		if (!em.contains(user)) {
			tmpUsr = (User)em.merge(user);
//			em.flush();
//			em.refresh(tmpUsr);
		}
		em.merge(tmpUsr);
	}
	
	@Override
	public boolean isAdmin(int userId) {
		User tmpuser = null;
		tmpuser = findByPK(userId);
		if (tmpuser!=null)
			return tmpuser.isAdmin();
		return false;
	}
}
